<?php
/**
 * 定时任务 - 活动结束后五分钟结算先到先得
 */
namespace Core\Controller;
use Think\Controller;
use  Common\Logic\GroupbuyLogic;

header("content-type:application/json");
set_time_limit(0);

class SpellOrderController extends Controller 
{
	/**
	 * 处理已经完成了的活动
	 * 每五分钟处理一次
	 * https://dstest.pxuc.com.cn/index.php/Core/SpellOrder/index
	 */
    public function index() 
    {
		$ntime = \time();
		$where_spell_project['conduct_end_time'] = ['LT',time()];
		$where_spell_project['state'] = 1;
		$where_spell_project['settlement_user_id'] = 0;
		$spell_project = M('spell_project')->where($where_spell_project)->find();
		if($spell_project){
			// 重新结算所有数据
			$where_spell_project_user['spell_project_id'] = $spell_project['spell_project_id'];
			foreach(M('spell_project_user')->where($where_spell_project_user)->select() as $k=>$v){
				update_spell_project($v['spell_project_id'],$v['user_id']);
			}
			// 获取第一名信息
			$spell_project_user = M('spell_project_user')->where($where_spell_project_user)->order('point desc')->find();
			if($spell_project_user['point'] > 0){
				// 写入活动信息表
				M('spell_project')->where(['spell_project_id'=>$spell_project['spell_project_id']])->save(['settlement_user_id'=>$spell_project_user['user_id']]);
				echo \date("Y-m-d H:i:s",\time())."先到先得活动结算：结算成功".$spell_project_user['user_id'].PHP_EOL;
			}else{
				// 活动延期
				M('spell_project')->where(['spell_project_id'=>$spell_project['spell_project_id']])->save(['conduct_end_time'=>time()+60*60*24]);
				echo \date("Y-m-d H:i:s",\time())."先到先得活动结算：延期".$spell_project_user['user_id'].PHP_EOL;
			}
			
		}else{
			echo \date("Y-m-d H:i:s",\time())."先到先得活动结算：无可用结算".PHP_EOL;
		}
		
	}
	
	/**
	 * 推送
	 * 每五分钟处理一次
	 * https://dstest.pxuc.com.cn/index.php/Core/SpellOrder/pushMessage
	 */
	public function pushMessage() 
	{
		$ntime = \time();
		$where_spell_project['wait_end_time'] = ['LT',time()-60*5];
		$where_spell_project['settlement_user_id'] = 0;
		$where_spell_project['state'] = 1;
		$spell_project = M('spell_project')->where($where_spell_project)->select();
		if(count($spell_project) > 0){
			$miniapp_conf = C('miniapp');
			
			$res_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$miniapp_conf['appid']}&secret={$miniapp_conf['appsecret']}";
			if($res_token['errcode'] == 0){
				$access_token = $res_token['access_token'];
				
				// 重新结算所有数据
				foreach($spell_project as $k => $v){
					// 查询用户
					$where_user['spell_project_id'] = $v['spell_project_id'];
					
					$user_list = M('spell_project_user')->where($where_user)->select();
					foreach($user_list as $ks => $vs){
						// 查询推送标识
						$user_formid['user_id'] = $vs['user_id'];
						$formid = M('miniapp_formid')->where($user_formid)->find();
						if($formid){
							// 查询用户信息
							$where_user_data['user_id'] = $vs['user_id'];
							$user_data = M('users')->where($where_user_data)->find();
							if($user_data){
								$url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send";
								$push_message['access_token'] = $access_token;
								$push_message['touser'] = $user_data['openid'];
								$push_message['template_id'] = 'l08yO__I5D-BGokKUM9SjoCv2L0T2HZlYG1OnycvZH8';
								$push_message['form_id'] = $formid['fromid'];
								$data['keyword1'] = ['value'=>date("Y年m月d日 H:i")];
								$data['keyword2'] = ['value'=>'先到先得活动已经开启！'];
								$push_message['data'] = $data;
								httpPost($url ,\json_encode($push_message));
								// 删除本人剩余的id
								M('miniapp_formid')->where($user_formid)->delete();
							}
						}
					}
				}
			}
			
		}else{
			echo \date("Y-m-d H:i:s",\time())."先到先得推送：无可用推送".PHP_EOL;
		}
		
	}
	
	/**
	 * 对参与人数不够的进行延期
	 * 每1分钟处理一次
	 * https://dstest.pxuc.com.cn/index.php/Core/SpellOrder/checkActivity
	 */
	public function checkActivity() 
	{
		$ntime = \time();
		// 获取五分钟后开始的活动
		$where_spell_project['wait_end_time'] = ['BETWEEN','"'.($ntime - 60*10).",".(strtotime(date('YmdHis'))+60*60).'"'];
		$where_spell_project['settlement_user_id'] = 0;
		$where_spell_project['state'] = 1;
		$spell_project = M('spell_project')->where($where_spell_project)->select();
		// p($spell_project);die;
		foreach($spell_project as $k => $v){
			// 查询活动商品
			$where_spell_goods['spell_goods_id'] = $v['spell_goods_id'];
			$spell_goods = M('spell_goods')->where($where_spell_goods)->find();
			if($spell_goods['need_number'] > M('spell_project_user')->where(['spell_project_id'=>$v['spell_project_id']])->count()){
				$save_spell_project['wait_end_time'] = $ntime+60*60*24;
				$save_spell_project['last_update'] = \time();
				$save_spell_project['conduct_end_time'] = $ntime+(60*60*24*$spell_goods['conduct_time']);
				if(M('spell_project')->where(['spell_project_id'=>$v['spell_project_id']])->save($save_spell_project)){
					echo \date("Y-m-d H:i:s",\time())."先到先得活动延期成功".$v['spell_project_id'].PHP_EOL;
				}
			}			
		}
	}
	
	/**
	 * 对活跃度不够的进行延期
	 * 每1分钟处理一次
	 * https://dstest.pxuc.com.cn/index.php/Core/SpellOrder/checkActivityLevel
	 */
	public function checkActivityLevel() 
	{
		$ntime = \time();
		$otime = \strtotime(\date('Ymd'));
		// 
		$where_spell_project['activity_query_time'] = ['LT',$otime];
		$where_spell_project['wait_end_time'] = ['LT',$otime];
		$where_spell_project['settlement_user_id'] = ['EQ',0];
		$where_spell_project['state'] = 1;
		$spell_project = M('spell_project')->where($where_spell_project)->find();
		if($spell_project){
			$atime = \intval(($otime-$spell_project['wait_end_time'])/60/60);
			// 查询昨天参加的人数
			$where_user_list['spell_project_id'] = $spell_project['spell_project_id'];
			$where_user_list['add_time'] = ['LT',$otime];
			$user_list = M('spell_project_user')->where($where_user_list)->group('user_id')->select();
			// p($spell_project);p($user_list);die;
			$user_count = \count($user_list);
			// 查询用户昨天有没有下单
			$where_order_list['pay_time'] = ['ELT',$otime];
			$where_order_list['pay_time'] = ['EGT',$otime-60*60*24];
			$where_order_list['pay_status'] = 1;
			$where_order_list['order_sn'] = ['LIKE','M%'];
			$where_order_list['order_amount'] = ['GT',0];
			$order_list = M('order')->where($where_order_list)->group('user_id')->select();
			$order_count = \count($order_list);
			// 查询昨天充值的
			// 查询用户昨天有没有下单
			$where_recharge_list['pay_time'] = ['ELT',$otime];
			$where_recharge_list['pay_time'] = ['EGT',$otime-60*60*24];
			$where_recharge_list['pay_status'] = 1;
			$where_recharge_list['order_amount'] = ['GT',0];
			$recharge_list = M('recharge')->where($where_recharge_list)->group('user_id')->select();
			$recharge_count = \count($recharge_list);
			$o_pp = [];
			foreach($order_list as $k => $v){
				$o_pp[$v['user_id']] = 1;
			}
			foreach($recharge_list as $k => $v){
				$o_pp[$v['user_id']] = 1;
			}
			$o_pp_count = count($o_pp);
			// $o_pp_count = 1;
			$o_pp_count_c = \intval($o_pp_count / $user_count * 100);
			// p($atime);p($o_pp);die;
			if($atime <= 12){
				// 直接结算
				$save_project['activity_query_time'] = \time();
				$save_project['activity_level'] = $o_pp_count_c;
				$save_project['state'] = 1;
				$save_project['last_update'] = \time();
				$save_project['delay_number'] = 0;
				if(M('spell_project')->where(['spell_project_id'=>$spell_project['spell_project_id']])->save($save_project)){
					echo \date("Y-m-d H:i:s",\time())."先到先得活跃度结算成功".$spell_project['spell_project_id'].PHP_EOL;die;
				}
			}else{
				
				$save_project['activity_query_time'] = \time();
				// p($user_count);die;
				if($o_pp_count_c > 40){
					$save_project['delay_number'] = 0;
				}else{
					
					$save_project['activity_query_time'] = \time()+60*60*24;
					$save_project['delay_number'] = $spell_project['delay_number']+1;
					$save_project['conduct_end_time'] = $spell_project['conduct_end_time']+(60*60*24*5);
				}
				$save_project['state'] = 1;
				if($save_project['delay_number'] >=3){
					$save_project['state'] = 0;
				}
				// 直接结算
				$save_project['activity_level'] = $o_pp_count_c;
				$save_project['last_update'] = \time();
				
				if(M('spell_project')->where(['spell_project_id'=>$spell_project['spell_project_id']])->save($save_project)){
					echo \date("Y-m-d H:i:s",\time())."先到先得活跃度结算成功".$spell_project['spell_project_id'].PHP_EOL;die;
				}
			}
			
			
		}else{
			echo \date("Y-m-d H:i:s",\time())."无活动".PHP_EOL;die;
		}
		
	}
    
}